package yiminsu.com.cn.controller;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import yiminsu.com.cn.utils.FileUtils;
import yiminsu.com.cn.beans.HotelstoryDictionaries;
import yiminsu.com.cn.beans.HotelTypeAssort;
import yiminsu.com.cn.beans.HotelTypeBedType;
import yiminsu.com.cn.beans.HotelTypeMoney;
import yiminsu.com.cn.service.HostUserService;
import yiminsu.com.cn.utils.ResultsUtil;
import yiminsu.com.cn.utils.UUIDUtils;

@RestController
@RequestMapping("/room")
public class RoomController {

	@Autowired
	private HostUserService hostUserService;
	
	private ResultsUtil resultsUtil;
	
	/**
	 * 新增房型   
	 * @return 
	 */
	@RequestMapping("/addRoomEditor")
	@Transactional
	public ResultsUtil addRoomEditor(@RequestParam("hotelTypeId") Integer hotelTypeId,
			    String hotelTypeName,
			    @RequestParam(value="files",required=false)MultipartFile[] files,Integer rooms,
			    Integer roomt,
			    Integer roomw,Integer roomc,String roomarea,Integer residepersonnummax,
			    String roomdescribe,
			    HotelTypeBedType hotelTypeBedType,HotelTypeMoney hotelTypeMoney,
			    HotelstoryDictionaries hotelStoryDictionaries,
			    HotelTypeAssort hotelTypeAssort)
	{		
		int insertRoom=0;	
		Map<String,Object> map=new HashMap<String, Object>();
		//处理主表的图片
		StringBuffer hotelImage=new StringBuffer();
		List<String> listimage= FileUtils.Fileupload(files);//中间表list
		for (int i = 0; i < listimage.size(); i++) {
			hotelImage.append(listimage.get(i)+"##");
		}
		insertRoom = hostUserService.insertRoom(hotelTypeId, hotelTypeName, listimage,hotelImage.toString(),rooms, roomt, roomw, roomc,
				roomarea,residepersonnummax, roomdescribe);
		map.put("insertRoom", insertRoom);
		
	    /**
	     * 配置设施插入房间的流程
	     */
		String storyId = hotelStoryDictionaries.getId();//配套设施id="1,2,3,4"
		if(storyId == "") {
			System.out.println("没有获取到配套设施数据");
		}else {
			int indexOf = storyId.indexOf(",");//判断该字符串中是否存在逗号(,)
			//不存在,说明就是一个id
			if(indexOf == -1) {
				int selectId = hostUserService.selectById(storyId);//根据id查询字典表是否存在数据select count(*) from 字典表 where id = #{s}
				if(selectId == 0){
					int insertDictionaries = hostUserService.insertDictionaries(hotelStoryDictionaries);//如果字典表中没有这条数据就插入一条字典数据
					if(insertDictionaries==0) 
					{
						 resultsUtil.setMsg("新增失败");
						 resultsUtil.setType(false);						
					}else
					{
						hostUserService.insertAssort(UUIDUtils.UUIDTo32(),hotelTypeId,storyId);//根据该id去插入 配置设施表数据
					}
					
				    }else
				    {
					    hostUserService.insertAssort(UUIDUtils.UUIDTo32(),hotelTypeId,storyId);//根据该id去插入 配置设施表数据
				    }
				
			}else {
				//存在逗号说明是多个id
				String[] split = storyId.split(",");//根据,分割数据,因为不止一个id split[1,2,3,4]
				if(split.length > 1){
					//循环id
					for (String s : split) {
						int selectId = hostUserService.selectById(storyId);//根据id查询字典表是否存在数据select count(*) from 字典表 where id = #{s}
						if(selectId == 0){
							hotelStoryDictionaries.setId(s);
							int insertDictionaries = hostUserService.insertDictionaries(hotelStoryDictionaries);//如果字典表中没有这条数据就插入一条字典数据
							if(insertDictionaries == 0) 
							{								
								resultsUtil.setMsg("新增失败");
								resultsUtil.setType(false);									
							}else 
							{								
								hostUserService.insertAssort(UUIDUtils.UUIDTo32(),hotelTypeId,s);//根据该id去插入 配置设施表数据															    	
							}
							
						}else{;
							hotelStoryDictionaries.setId(s);
							hostUserService.insertAssort(UUIDUtils.UUIDTo32(),hotelTypeId,s);//根据该id去插入 配置设施表数据
						}
					}
				}
		}	
	}
		
		/**
		 * 房间价格插入房间的流程
		 */
		//获取价格的字典表Id
		String priceId=hotelTypeMoney.getHdid();
		//获取房间价格
		BigDecimal money=hotelTypeMoney.getHmoney();
		//将价格转换为String类型
		String price = money.toString();
		String[] pid=priceId.split(",");//根据","分割数据  pid split[1,2]
		String[] tariff=price.split(",");//tariff split[200,500]
		//循环
		for (int i = 0; i < pid.length; i++) {
			String ids = pid[i];
			String moneys = tariff[i]; 
			int selectId = hostUserService.selectById(ids);//根据id查询字典表是否存在数据select count(*) from 字典表 where id = #{s}
			//将价格转换为BigDecimal类型
			BigDecimal prices=new BigDecimal(moneys);
			if(selectId == 0) 
			{
				hotelStoryDictionaries.setId(ids);				
				int insertDictionaries = hostUserService.insertDictionaries(hotelStoryDictionaries);//如果字典表中没有这条数据就插入一条字典数据
		        if(insertDictionaries == 0) 
		        {
		        	resultsUtil.setMsg("新增失败");
					resultsUtil.setType(false);			        			        	
		        }else
		        {
		        	//新增成功后往价格表插入一条数据
		        	hostUserService.insertMoney(UUIDUtils.UUIDTo32(),hotelTypeId, ids, prices);
		        }
				
			}else 
			{
				hotelStoryDictionaries.setId(ids);
			    //如果字典表中就往房间价格表插入数据
			    hostUserService.insertMoney(UUIDUtils.UUIDTo32(),hotelTypeId, ids, prices);		
			}
		}
		
		
		/**
		 * 房型插入房间的流程
		 */
		//获取房型的字典表Id
		String typeId=hotelTypeBedType.getDid();		
		//根据Id查询房型表是否存在这一条数据
		int  gainId=hostUserService.selectById(typeId);
		String dmsid = hotelTypeBedType.getDmsid();
		int bedtypenum = hotelTypeBedType.getBedtypenum();
		if(gainId == 0) 
		{
			hotelStoryDictionaries.setId(typeId);
			int insertDictionaries = hostUserService.insertDictionaries(hotelStoryDictionaries);//如果字典表中没有这条数据就插入一条字典数据
			if(insertDictionaries ==0 )
			{				
				resultsUtil.setMsg("新增失败");
				resultsUtil.setType(false);	
			}else
			{
				hostUserService.insertRoomType(UUIDUtils.UUIDTo32(),hotelTypeId, typeId,dmsid,bedtypenum);//再往房型表里面插入数据			
			}
								
		}else 
		{			
			hotelStoryDictionaries.setId(typeId);
		    //如果字典表中有就往房型表里面插入数据	
			hostUserService.insertRoomType(UUIDUtils.UUIDTo32(),hotelTypeId, typeId,dmsid,bedtypenum);
		}		
		return resultsUtil;
	}
			
	/**
	 * 点击编辑回显数据
	 */
	@RequestMapping("/echoData")
	@ResponseBody
	public ResultsUtil echoData(@RequestParam("hotelTypeId") Integer hotelTypeId,Model model) 
	{	        
		System.out.println(System.getProperty("user.dir"));	
		//查询房间信息
		model.addAttribute("hotelType",hostUserService.selectHotelType(hotelTypeId));
		//查询房间床型信息
		model.addAttribute("hotelTypeBedType",hostUserService.selectHotelBed(hotelTypeId));
		//查询房间价格信息
		model.addAttribute("hotelTypeMoney",hostUserService.selectMoney(hotelTypeId));
		//查询房间配套设施信息
		model.addAttribute("hotelTypeAssort",hostUserService.selectAssort(hotelTypeId));	
		resultsUtil.setData(model);
		resultsUtil.setMsg("查询成功！");
		resultsUtil.setType(true);
		return resultsUtil;
	}
	
	/**
	 * 房型信息编辑
	 */	
	@RequestMapping("/changePreview")
	@Transactional
	public ResultsUtil changePreview(@RequestParam("hotelTypeId") Integer hotelTypeId,String hotelTypeName,
		    @RequestParam(value="files",required=false)MultipartFile[] files,Integer rooms,Integer roomt,
		    Integer roomw,Integer roomc,String roomarea,Integer residepersonnummax,String roomdescribe,
		    String did,String dmsid,Integer bedtypenum,String dvalue,String hdid,BigDecimal hmoney,String tdid) 
	{
		/**
	     * 修改房间表	
		 */
		int updateRoom=0;
		Map<String,Object> map=new HashMap<String, Object>();
		//处理主表的图片
		StringBuffer hotelImage=new StringBuffer();
		List<String> listimage= FileUtils.Fileupload(files);//中间表list
		for (int i = 0; i < listimage.size(); i++) {
			hotelImage.append(listimage.get(i)+"##");
		}
		//修改房间
		updateRoom=hostUserService.updateRoom(hotelTypeId,hotelTypeName,listimage,hotelImage.toString(),rooms,roomt,
				roomw,roomc,roomarea,residepersonnummax,roomdescribe);
	    map.put("updateRoom",updateRoom);
		
		/**
		 * 修改房间房型表
		 */
		//修改房间床型
		int updateRoomType = hostUserService.updateRoomType(hotelTypeId,did,dmsid,bedtypenum);
		if(updateRoomType==0) 
		{
			 resultsUtil.setMsg("修改房间类型表失败");
			 resultsUtil.setType(false);						
		}else
		{
			//修改字典表
			hostUserService.updateDictionaries(did, dvalue);
		}
		
		/**
		 * 修改房间价格
		 */
		//将价格转换为String类型
		String price = hmoney.toString();
		String[] pid=hdid.split(",");////获取价格的字典表Id,根据","分割数据  pid split[1,2]
		String[] tariff=price.split(",");//获取房间价格,根据","分割数据tariff split[200,500]
		//循环
		for (int i = 0; i < pid.length; i++) {
			String ids = pid[i];
			String moneys = tariff[i]; 
			//将价格转换为BigDecimal类型
			BigDecimal prices=new BigDecimal(moneys);
			hostUserService.updatePrice(hotelTypeId,ids,prices);
		}
		
		/**
		 * 修改配套设施
		 */
		String[] split = tdid.split(",");//根据,分割数据,因为不止一个id split[1,2,3,4]
		if(split.length > 1){
			//循环字典表id
			for (String motorId: split) {
              hostUserService.updateAssort(hotelTypeId, motorId);       
		 }
      }	
		return resultsUtil;
	}
	
	/**
	 * 批量设置房态
	 */
	@RequestMapping("api/v1/changeRoomNumber") 
	@Transactional
	public ResultsUtil changeRoomNumber(@RequestParam("hotelTypeId") Integer hotelTypeId,
			Integer roomstatus,Date roomtime) 
	{			
		return hostUserService.updateStatus(hotelTypeId, roomstatus, roomtime);
	}
	
	/**
	 * 批量设置价格
	 */
	@RequestMapping("api/v1/modifyPrice")
	public ResultsUtil modifyPrice(@RequestParam("hotelTypeId") Integer hotelTypeId,BigDecimal hmoney,Date roomtime)
	{
        HotelTypeMoney htMoney=new HotelTypeMoney();
        //获取字典表Id
        String hdid = htMoney.getHdid();
        //给房间价格表添加数据(uuId,房间Id,字典表Id,房间价格)
		int insertHotelPrice = hostUserService.insertHotelPrice(UUIDUtils.UUIDTo32(), hotelTypeId,hdid,hmoney);
		if(insertHotelPrice==0) 
		{
			 resultsUtil.setMsg("新增价格失败"); 
			 resultsUtil.setType(false);				
		}else
		{
			HotelstoryDictionaries htDictionaries=new HotelstoryDictionaries();
            //获取字典表中的值
			String dname = htDictionaries.getDname();
			//获取字典表中的类型
			Integer dtype = htDictionaries.getDtype();
			//将时间类型转换为String类型
			String dateRoom = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(roomtime);
			//给字典表添加数据(字典表Id,字典名称,值,是否能用2代表活动价格)
			hostUserService.insertStroyValue(hdid, dname,dateRoom, dtype);
		}		
	     return resultsUtil;
	}
	 	
}